package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Activity;
import cn.marinda.resturants.exception.ResourceNotFoundException;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.ActivityMapper;
import cn.marinda.resturants.service.ActivityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/** 活动服务实现类
 * @date 2021/11/18
 * @author Marinda
 *
 */
@Service("activityService")
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper,Activity>  implements ActivityService {

    /**
     * 查询所有活动列表数据
     *
     * @return 活动列表
     * @date 2021/11/18
     */
    @Override
    public List<Activity> queryList() {
        return baseMapper.queryList();
    }

    /**
     * 通过查询uid来返回一个活动列表
     *
     * @param uid 列表uid
     * @return 活动
     */
    @Override
    public Activity selectByUid(String uid) {
        return baseMapper.selectByUid(uid);
    }

    /**
     * 删除由uid
     *
     * @param uid uid
     * @return boolean
     */
    @Override
    public boolean deleteActivity(String uid){
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 通过模糊查询内容来获取某个活动
     *
     * @param content 内容
     * @return 活动
     */
    @Override
    public Activity selectContentLike(String content) {
        return baseMapper.selectContentLike(content);
    }

    /**
     * 通过模糊查询标题来获取某个活动
     *
     * @param title 标题
     * @return 活动
     */
    @Override
    public Activity selectTitleLike(String title) {
        return baseMapper.selectTitleLike(title);
    }

    /**
     * 通过模糊查询链接来找到这个活动
     *
     * @param href 链接
     * @return 活动
     */
    @Override
    public Activity selectHrefLike(String href) {
        return baseMapper.selectHrefLike(href);
    }

    /**
     * 增加一个活动
     *
     * @param activity 活动
     * @return 活动
     */
    @Override
    public boolean addActivity(Activity activity) {
        activity.setUid(UUID.randomUUID().toString());
        return baseMapper.insert(activity)>0;
    }

    /**
     * 一个悲观锁查询
     * 要加事务处理回滚操作。
     *
     * @param uid
     * @date 2021/11/18
     * @author Marinda
     */
    @Override
    public Activity selectPessimisticLock(String uid) {
        return baseMapper.selectPessimisticLock(uid);
    }

    /**
     * 一个乐观锁修改数据
     *
     * @param activity 活动
     * @return 活动
     */
    @Override
    public boolean updateOptimisticLock(Activity activity) {
        Activity newActivice = baseMapper.selectByUid(activity.getUid());
        if(newActivice == null){
            try {
                throw new ResourceNotFoundException("资源不存在");
            } catch (ResourceNotFoundException e) {
                e.printStackTrace();
            }
        }
        activity.setVersion(newActivice.getVersion());
        return lambdaUpdate().eq(Activity::getUid,activity.getUid()).eq(Activity::getVersion,activity.getVersion()).set(Activity::getVersion,activity.getVersion()+1).update(activity);

    }

    /**
     * 分页
     *
     * @param pages 分页
     * @return 分页结果
     */
    @Override
    public IPage<Activity> selectActivityPages(Pages<?> pages) {
        return baseMapper.selectActivityPages(pages);
    }

}
